<?php
GLOBAL $USE_JSON; 
require_once('../conf/setup.inc');
require_once(CORE_DIR.'url_mappings.inc');
require_once(CORE_DIR.'utils.php');
require_once(CORE_DIR.'Action.php');
require_once(CORE_DIR.'authAction.php');
require_once(CORE_DIR.'View.php');
require_once(CORE_DIR.'Model.php');
require_once(CORE_DIR.'ErrorHandler.php');

function __autoload($className) {
	if(stripos($className,'dto') !== false) {
		require_once(DTO_DIR.$className.'.php');
	}
}


//$sTime = microtime(true);
$eh = new ErrorHandler();
$eh->init();

parseUrl();

//echo microtime(true) - $sTime;
return;


/*************************************************************************/
/**************************URL PARSING FUNCTIONS******************************/
/*************************************************************************/

function parseUrl() {
	$tokens = $_GET;

	$modulePair = UrlMapping::getModulePair($tokens['cmd']);

	$module = $modulePair[0];
	$action = $modulePair[1];

	$viewType = (isset($tokens['type'])) ? $tokens['type'] : VS_NORMAL;
	$filename = MODULES_DIR.$module.'/'.$action.'Action.php';
	$class = $action.'Action';

	if($module == "mailbody")
	{
		header('Content-Type: text/html; charset=utf-8');
		$viewType = VS_PLUGIN;
	}
	else {
		GLOBAL $USE_JSON;
		$USE_JSON = false;
		if (isset($_GET["usejson"]) && $_GET["usejson"]==1){
			$USE_JSON = true;
		}
		if (!$USE_JSON){
			header('Content-Type: text/xml; charset=utf-8');
		}

	}



	if(isset($tokens['sid']) && $tokens["sid"]!= null && $tokens["sid"]!= "null" && $tokens["sid"]!= "")
	session_id($tokens["sid"]);
	session_start();

	if(!file_exists($filename)) {
		$filename = MODULES_DIR.'error/pagenotfoundAction.php';
		$class = 'pagenotfoundAction';
		$viewType = VS_PLUGIN;
	}

	require_once($filename);

	$actionClass = new $class;
	$actionClass->run($tokens, $_POST, $viewType);
}
?>